package com.zh.ssmshirofreemarker.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.zh.ssmshirofreemarker.Utils.ResponseUtil;
import com.zh.ssmshirofreemarker.entity.JsTree;
import com.zh.ssmshirofreemarker.entity.Permission;
import com.zh.ssmshirofreemarker.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZH
 * @since 2018-07-24
 */
@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    IPermissionService permissionService;

    @ResponseBody
    @RequestMapping(value = "/findAll")
    public Page<Permission> getPage(Page<Permission> page, Permission permission){
        return permissionService.getPermissionPage(page,permission);
    }

    @ResponseBody
    @RequestMapping(value = "/jsTree")
    public List<JsTree> getJsTrees(){
        return permissionService.getAllMenuForJson();
    }

    @RequestMapping("/add")
    public String addView(Map<String,Object> map){
        map.put("type","add");
        return "permission/add-edit";
    }

    @RequestMapping(method = RequestMethod.GET)
    public String init(){
        return "permission/init";
    }

    @ResponseBody
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public Permission findPermission(@PathVariable("id")Integer id){
        return permissionService.selectById(id);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public ResponseUtil doAdd(Permission permission){
        return ResponseUtil.check(permissionService.doAdd(permission));
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.PUT)
    public ResponseUtil doUpdate(Permission permission){
         return ResponseUtil.check(permissionService.doUpdate(permission));
    }

    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable("id")Integer id,Map<String,Object> map){
        Permission permission = permissionService.selectById(id);
        Permission permission1 = permissionService.selectById(permission.getParentId());
        if( permission1 == null){
            map.put("parentName","");
        } else {
            map.put("parentName",permission1.getPermissionName());
        }
        map.put("permission",permission);
        map.put("type","edit");
        return "permission/add-edit";
    }


    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE)
    public ResponseUtil doDelete(String ids){
        boolean result = false;
        try{
            result = permissionService.deletePermisisonByIds(
                     Arrays.asList(
                        ids.split(",")
                     )
                    );
        } catch (Exception e){
            e.printStackTrace();
        }
        return ResponseUtil.check(result);
    }


}

